<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <script>
   
        function dateFormat(time) {

            let nowtime = new Date()
            let oldtime = new Date(time)
            let times = (nowtime.getTime() - oldtime.getTime()) / 1000

            if (times < 10) {
                return "刚刚"
            } else if (times >= 10 && times < 60) {
                return parseInt(times) + '秒前'
            } else if (times >= 60 && times <3600) {
                return parseInt(times / 60) + '分钟前'
            } else if (times >= 3600&& times<86400) {
                return parseInt(times / 60 / 60) + '小时前'
            } else if (times >=86400) {
                return parseInt(times/60/60/24)+'天前'
            }
        }
        console.log(dateFormat('2021-7-26 11:29:21'));
    </script>
</body>

</html>